﻿using System;
using System.Globalization;
using System.Web.UI;
using System.Web.UI.WebControls;
using AB.ServiceDirectory.BO;
using AB.ServiceDirectory.Commons.DataFilters;

namespace AB.ServiceDirectory.Design.WebUserControls.Lookup
{
    public partial class AddressLookup : UserControl
    {
        /// <summary>
        /// Gets the text box postcode ID.
        /// </summary>
        public string TextBoxPostcodeId
        {
            get
            {
                if (ViewState["TextBoxPostcodeId"] != null)
                {
                    return ViewState["TextBoxPostcodeId"] as string;
                }
                return string.Empty;
            }
            set { ViewState["TextBoxPostcodeId"] = value; }
        }

        /// <summary>
        /// Gets the address line1.
        /// </summary>
        public string TextBoxAddressLine1Id
        {
            get
            {
                if (ViewState["TextBoxAddressLine1Id"] != null)
                {
                    return ViewState["TextBoxAddressLine1Id"] as string;
                }
                return string.Empty;
            }
            set { ViewState["TextBoxAddressLine1Id"] = value; }
        }

        /// <summary>
        /// Gets the address line2.
        /// </summary>
        public string TextBoxAddressLine2Id
        {
            get
            {
                if (ViewState["TextBoxAddressLine2Id"] != null)
                {
                    return ViewState["TextBoxAddressLine2Id"] as string;
                }
                return string.Empty;
            }
            set { ViewState["TextBoxAddressLine2Id"] = value; }
        }

        /// <summary>
        /// Gets the address line3.
        /// </summary>
        public string TextBoxAddressLine3Id
        {
            get
            {
                if (ViewState["TextBoxAddressLine3Id"] != null)
                {
                    return ViewState["TextBoxAddressLine3Id"] as string;
                }
                return string.Empty;
            }
            set { ViewState["TextBoxAddressLine3Id"] = value; }
        }

        /// <summary>
        /// Gets the hidden field address ID.
        /// </summary>
        public string HiddenFieldAddressId
        {
            get
            {
                if (ViewState["HiddenFieldAddressId"] != null)
                {
                    return ViewState["HiddenFieldAddressId"] as string;
                }
                return string.Empty;
            }
            set { ViewState["HiddenFieldAddressId"] = value; }
        }

        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                var filter = new AddressFilter();
                ViewState["filter"] = filter;
            }
        }

        /// <summary>
        /// Buttons the search click.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void ButtonSearchClick(object sender, EventArgs e)
        {
            var filter = ViewState["filter"] as AddressFilter;
            if (filter != null)
            {
                filter.Postcode = TextBoxPostcode.Text;
                filter.Street = TextBoxStreet.Text;
                filter.Town = TextBoxTown.Text;
            }
            GridViewSearchPostcode.DataBind();
        }

        /// <summary>
        /// Buttons the clear click.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void ButtonClearClick(object sender, EventArgs e)
        {
            TextBoxTown.Text = string.Empty;
            TextBoxStreet.Text = string.Empty;
            TextBoxPostcode.Text = string.Empty;
        }

        /// <summary>
        /// Buttons the none click.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void ButtonNoneClick(object sender, EventArgs e)
        {
            var addressIdControl = Parent.FindControl(HiddenFieldAddressId) as HiddenField;
            if (addressIdControl != null)
            {
                addressIdControl.Value = string.Empty;
            }

            var postcodeControl = Parent.FindControl(TextBoxPostcodeId) as TextBox;
            if (postcodeControl != null)
            {
                postcodeControl.Text = string.Empty;
            }

            var addressLine1Control = Parent.FindControl(TextBoxAddressLine1Id) as TextBox;
            if (addressLine1Control != null)
            {
                addressLine1Control.Text = string.Empty;
            }

            var addressLine2Control = Parent.FindControl(TextBoxAddressLine2Id) as TextBox;
            if (addressLine2Control != null)
            {
                addressLine2Control.Text = string.Empty;
            }

            var addressLine3Control = Parent.FindControl(TextBoxAddressLine3Id) as TextBox;
            if (addressLine3Control != null)
            {
                addressLine3Control.Text = string.Empty;
            }

            PanelContainer.Visible = false;
        }

        /// <summary>
        /// Buttons the close click.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void ButtonCloseClick(object sender, EventArgs e)
        {
            PanelContainer.Visible = false;
        }

        /// <summary>
        /// Buttons the select click.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void ButtonSelectClick(object sender, EventArgs e)
        {
            var addressId = 0;
            if (!int.TryParse(Request.Form["RadioButton"], out addressId))
            {
                return;
            }

            var objAddress = AddressBO.GetAddressById(addressId);
            var addressIdControl = Parent.FindControl(HiddenFieldAddressId) as HiddenField;
            if (addressIdControl != null)
            {
                addressIdControl.Value = objAddress.AddressId.ToString(CultureInfo.CurrentCulture);
            }

            var postcodeControl = Parent.FindControl(TextBoxPostcodeId) as TextBox;
            if (postcodeControl != null)
            {
                postcodeControl.Text = objAddress.Postcode;
            }

            var addressLine1Control = Parent.FindControl(TextBoxAddressLine1Id) as TextBox;
            if (addressLine1Control != null)
            {
                addressLine1Control.Text = objAddress.AddressLine;
            }

            var addressLine2Control = Parent.FindControl(TextBoxAddressLine2Id) as TextBox;
            if (addressLine2Control != null)
            {
                addressLine2Control.Text = objAddress.AddressLine2;
            }

            var addressLine3Control = Parent.FindControl(TextBoxAddressLine3Id) as TextBox;
            if (addressLine3Control != null)
            {
                addressLine3Control.Text = objAddress.AddressLine3;
            }

            PanelContainer.Visible = false;
        }

        /// <summary>
        /// Addresses the object data source selecting.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.Web.UI.WebControls.ObjectDataSourceSelectingEventArgs"/> instance containing the event data.</param>
        protected void AddressObjectDataSourceSelecting(object sender, ObjectDataSourceMethodEventArgs e)
        {
            var filter = ViewState["filter"] as AddressFilter;
            e.InputParameters["filter"] = filter;
        }

        /// <summary>
        /// Popups this instance.
        /// </summary>
        public void Popup()
        {
            PanelContainer.CssClass = "popup_container";
            PanelContainer.Visible = true;
        }
    }
}